package com.github.cyf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.cyf.common.module.response.Rs;
import com.github.cyf.domain.po.Role;
import com.github.cyf.service.PermissionService;
import com.github.cyf.service.RolePermissionService;
import com.github.cyf.service.RoleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author chenyifan
 * @since 2024-10-16
 */
@RestController
@RequestMapping("/role")
@ApiSupport(order = 3)
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @PostMapping("/addOrUpdateRole")
    @ApiOperation("1、添加或修改角色信息")
    @ApiOperationSupport(order = 1)
    @SaCheckPermission(value = {"admin:role:insert", "admin:role:update"})
    public Rs addOrUpdateRole(@RequestBody @Validated Role role){
        return Rs.ok().setData(roleService.saveOrUpdate(role));
    }

    @GetMapping("/{id}")
    @ApiOperation("2、查询角色信息")
    @ApiOperationSupport(order = 2)
    @SaCheckPermission(value = "admin:role:select")
    public Rs getRole(@PathVariable Long id){
        return Rs.ok().setData(roleService.getById(id));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("3、删除角色")
    @ApiOperationSupport(order = 3)
    @SaCheckPermission(value = "admin:role:delete")
    public Rs deleteRole(@PathVariable Long id){
        return Rs.ok().setData(roleService.removeById(id));
    }

    @GetMapping("/getRolePermission/{id}")
    @ApiOperation("4、查询角色权限信息")
    @ApiOperationSupport(order = 4)
    @SaCheckPermission(value = "admin:role:select")
    public Rs getRolePermission(@PathVariable Long id){
        return Rs.ok().setData(permissionService.queryPermissionByRoleId(id));
    }

    @GetMapping("/selectAll")
    @ApiOperation("5、查询角色列表")
    @ApiOperationSupport(order = 5)
    @SaCheckPermission(value = "admin:role:select")
    public Rs selectRole(){
        return Rs.ok().setData(roleService.list());
    }

    @PostMapping("/saveRolePermission")
    @ApiOperation("6、保存角色权限信息")
    @ApiOperationSupport(order = 6)
    @SaCheckPermission(value = "admin:role:assign")
    public Rs saveRolePermission(Long roleId, @RequestParam("permissionIds") List<String> permissionIds){
        return Rs.ok().setData(rolePermissionService.saveRolePermission(roleId, permissionIds));
    }
}
